import tools from "../../../utils/tools";
import cartItem from '../../../template/cartItem'
@tools
export default class Handle {
    constructor(courseData, cartData, oCatr, observer) {
        this.courseData = courseData;
        this.cartData = cartData;
        this.oCatr = oCatr;
        this.observer = observer;
        this.init();
    }
    init() {
        this.observer.add(this.handleCartItem.bind(this))
    }
    handleCartItem(id, target, state) {
        if (state) {
            const itemData = this.courseData.filter(item => {
                if (item.id === id) {
                    return item
                }
            })[0];
            const _item = Handle.createElement(
                'li',
                Handle.tplReplace(cartItem, {
                    id: itemData.id,
                    title: itemData.title,
                    priginPrice: itemData.coupon ? `<del>${itemData.price}</del>` : `${itemData.price}`,
                })
            )
            this.oCatr.appendChild(_item)
        } else {
            const oParent = target.parentNode;
            oParent.remove();
        }
    }
}